<?php

// $Id: template.php,v 1.4 2009/07/13 23:52:57 andregriffin Exp $

/**
 * Sets the body-tag class attribute.
 *
 * Adds 'sidebar-left', 'sidebar-right' or 'sidebars' classes as needed.
 */
function framework_body_class($left, $right) {
    $class = array();

    if ($left != '' && $right != '') {
        $class[] = 'sidebars';
    } elseif ($left != '') {
        $class[] = 'sidebar-left';
    } elseif ($right != '') {
        $class[] = 'sidebar-right';
    }

    if (arg(0) == 'admin') {
        $class[] = 'admin';
    }

    if ($class) {
        print ' class="' . implode(' ', $class) . '"';
    }
}

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
    if (!empty($breadcrumb)) {
// uncomment the next line to enable current page in the breadcrumb trail
//    $breadcrumb[] = drupal_get_title();
        return '<div class="breadcrumb">' . implode(' » ', $breadcrumb) . '</div>';
    }
}

/**
 * Allow themable wrapping of all comments.
 */
function framework_comment_wrapper($content, $node) {
    if (!$content || $node->type == 'forum') {
        return '<div id="comments">' . $content . '</div>';
    } else {
        return '<div id="comments"><h2 class="comments">' . t('Comments') . '</h2>' . $content . '</div>';
    }
}

function framework_preprocess_views_view__page(&$vars) {

    foreach ($vars['view']->style_plugin->rendered_fields as $field) {
        $result[] = array(
            'title' => $field['title'],
            'teaser' => substr($field['field_description_value'], 0, 300),
            'price' => $field['field_price_value'],
            'path' => $field['path'],
            'image' => $field['field_image_gallery_fid']
        );
    }

    $vars['data_buy'] = $result;
}

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function framework_preprocess_page(&$vars) {

    if (($vars['template_files'][0] == 'page-aboutus') || ($vars['template_files'][0] == 'page-contactus')) {
        switch ($vars['template_files'][0]) {
            case 'page-aboutus':
                $node = node_load(50);
                break;
            case 'page-contactus':
                $node = node_load(51);
                break;
        }
        $vars['content'] = array(
            'title' => $node->title,
            'body' => $node->body
        );
    }


    $vars['tabs2'] = menu_secondary_local_tasks();
    $vars['path'] = base_path() . drupal_get_path('theme', 'framework');

    $data = taxonomy_get_tree(2);
    foreach ($data as $tree) {

        if ($tree->parents[0] == '0') {
            $children = taxonomy_get_children($tree->tid);
            $temp_child = array();
            $count = 0;
            if (count($children) > 0) {
                foreach ($children as $key => $child) {
                    if (++$count > 5)
                        break;
                    $temp_child[] = $child->name;
                }
                $vars['cars'][] = array(
                    'parent' => $tree->name,
                    'children' => $temp_child
                );
            }
        }
    }
    /*echo "<pre>";
    var_dump($vars['cars']);
    echo "</pre>";
    die;
     * *
     */


    if ($vars['is_front'] || ($vars['template_files'][0] == 'page-buy') || ($vars['template_files'][0] == 'page-aboutus') || ($vars['template_files'][0] == 'page-contactus')) {
        // var_dump($vars);
        $result_new = db_query("
            SELECT * from {content_type_automobile} c
            INNER JOIN {node} n ON n.nid = c.nid
            WHERE n.status = 1
            ORDER BY created DESC LIMIT 15");

        while ($row = db_fetch_object($result_new)) {
            $data = node_load($row->nid);
            $ary = explode('/',$data->field_image_gallery[0]['filepath']);
            $new_arrival[] = array(
                'title' => $data->title,
                'title_path' => $data->path,
                'image' => file_exists(file_directory_path() . '/imagecache/custom_imagecache/'. $ary[3]) ? file_directory_path() . '/imagecache/custom_imagecache/' . $ary[3] :  file_directory_path() . '/imagecache/custom_imagecache/' . $data->field_image_gallery[0]['filename']
            );
        }
        $vars['new'] = $new_arrival;

        $result_featured = @db_query("
            SELECT * from {content_type_automobile} c
            INNER JOIN {content_field_featured} n ON n.nid = c.nid
            WHERE field_featured_value = 1 ORDER BY n.nid DESC LIMIT 15");

        while ($row = @db_fetch_object($result_featured)) {
            $data = node_load($row->nid);
            $ary = explode('/',$data->field_image_gallery[0]['filepath']);
            $featured[] = array(
                'title' => $data->title,
                'title_path' => $data->path,
                'image' => file_exists(file_directory_path() . '/imagecache/custom_imagecache/'. $ary[3]) ? file_directory_path() . '/imagecache/custom_imagecache/' . $ary[3] :  file_directory_path() . '/imagecache/custom_imagecache/' . $data->field_image_gallery[0]['filename']
            );
        }
        $vars['featured'] = $featured;

        $result_most_viewed = db_query("
            SELECT * from {node} n
            INNER JOIN {node_counter} c ON n.nid = c.nid
            WHERE type = 'automobile' AND n.status = 1
            ORDER BY c.totalcount DESC LIMIT 6");

        while ($row = db_fetch_object($result_most_viewed)) {
            $data = node_load($row->nid);
            $ary = explode('/',$data->field_image_gallery[0]['filepath']);
            $most_viewed[] = array(
                'title' => $data->title,
                'title_path' => $data->path,
                'image' => file_exists(file_directory_path() . '/imagecache/custom_imagecache/'. $ary[3]) ? file_directory_path() . '/imagecache/custom_imagecache/' . $ary[3] :  file_directory_path() . '/imagecache/custom_imagecache/' . $data->field_image_gallery[0]['filename']
            );
        }
        $vars['most_viewed'] = $most_viewed;


        //load slideshow
        $nodes = node_load(21); //get the nid at content_type_slideshow
        //$data = node_load($nodes->field_content_1[0]['nid']);
        //var_dump($nodes);die;

        $vars['slides'] = array(
            'image_1' => $nodes->field_image_1[0]['filepath'],
            'image_1_thumb' => $nodes->field_image_1_thumb[0]['filepath'],
            'content_1_path' => drupal_get_path_alias('node/' . $nodes->field_content_1[0]['nid']),
            'image_2' => $nodes->field_image_2[0]['filepath'],
            'image_2_thumb' => $nodes->field_image_2_thumb[0]['filepath'],
            'content_2_path' => drupal_get_path_alias('node/' . $nodes->field_content_2[0]['nid']),
            'image_3' => $nodes->field_image_3[0]['filepath'],
            'image_3_thumb' => $nodes->field_image_3_thumb[0]['filepath'],
            'content_3_path' => drupal_get_path_alias('node/' . $nodes->field_content_3[0]['nid']),
            'image_4' => $nodes->field_image_4[0]['filepath'],
            'image_4_thumb' => $nodes->field_image_4_thumb[0]['filepath'],
            'content_4_path' => $nodes->field_articles_link[0]['value'],
            'image_5' => $nodes->field_image_5[0]['filepath'],
            'image_5_thumb' => $nodes->field_image_5_thumb[0]['filepath'],
            'content_5_path' => $nodes->field_contact_link[0]['value'],
        );
    } else {

        $result_recommended = @db_query("
            SELECT * from {content_type_automobile} c
            INNER JOIN {node} n ON n.nid = c.nid
            WHERE n.nid != " . $vars['node']->nid . " AND n.status = 1
            LIMIT 4");
        while ($row = @db_fetch_object($result_recommended)) {
            $data = node_load($row->nid);
            $recommended[] = array(
                'title' => $data->title,
                'title_path' => $data->path,
                'image' => file_directory_path() . '/imagecache/custom_imagecache/' . $data->field_image_gallery[0]['filename']
            );
        }
        $vars['recommended'] = $recommended;
        $vars['data'] = node_load($vars['node']->nid);
       
        $specs_name = array(
            "2" => "Model",
            "5" => "Piston Displacement",
            "7" => "Year",
            //"8" => "Price Range",
            "9" => "Transmission",
            "3" => "Color",
            "6" => "Engine",
            "10" => "Body Type"
         );
        $ctr = 0;
       // echo"<pre>"; var_dump($vars['data']->taxonomy);echo "</pre>";die;
        if ($vars['data']->taxonomy) {
           
            foreach (@$vars['data']->taxonomy as $key => $taxonomy) {
                if(in_array($taxonomy->vid, array_keys($specs_name)))
                    $specs[$specs_name[$taxonomy->vid]] = $taxonomy->name;
                //$ctr++;
            }

            $vars['specs'] = $specs;
        }
     //   echo"<pre>"; var_dump($vars['specs']);echo "</pre>";die;
    }
}

/**
 * Returns the rendered local tasks. The default implementation renders
 * them as tabs. Overridden to split the secondary tasks.
 *
 * @ingroup themeable
 */
function phptemplate_menu_local_tasks() {
    return menu_primary_local_tasks();
}

function framework_comment_submitted($comment) {
    return t('by <strong>!username</strong> | !datetime',
            array(
                '!username' => theme('username', $comment),
                '!datetime' => format_date($comment->timestamp)
    ));
}

function phptemplate_node_submitted($node) {
    return t('!datetime | by <strong>!username</strong>',
            array(
                '!username' => theme('username', $node),
                '!datetime' => format_date($node->created),
    ));
}

/**
 * Override or insert variables into the block templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("block" in this case.)
 */
function framework_preprocess_block(&$vars, $hook) {
    $block = $vars['block'];

    // Special classes for blocks.
    $classes = array('block');
    $classes[] = 'block-' . $block->module;
    $classes[] = 'region-' . $vars['block_zebra'];
    $classes[] = $vars['zebra'];
    $classes[] = 'region-count-' . $vars['block_id'];
    $classes[] = 'count-' . $vars['id'];

    $vars['edit_links_array'] = array();
    $vars['edit_links'] = '';
    if (user_access('administer blocks')) {
        include_once './' . drupal_get_path('theme', 'framework') . '/template.block-editing.inc';
        framework_preprocess_block_editing($vars, $hook);
        $classes[] = 'with-block-editing';
    }

    // Render block classes.
    $vars['classes'] = implode(' ', $classes);
}

/**
 * Generates IE CSS links.
 */
function framework_get_ie_styles() {
    $iecss = '<link type="text/css" rel="stylesheet" media="all" href="' . base_path() . path_to_theme() . '/ie.css" />';
    return $iecss;
}

function framework_get_ie6_styles() {
    $iecss = '<link type="text/css" rel="stylesheet" media="all" href="' . base_path() . path_to_theme() . '/ie6.css" />';
    return $iecss;
}

/**
 * Adds even and odd classes to <li> tags in ul.menu lists
 */
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
    static $zebra = FALSE;
    $zebra = !$zebra;
    $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
    if (!empty($extra_class)) {
        $class .= ' ' . $extra_class;
    }
    if ($in_active_trail) {
        $class .= ' active-trail';
    }
    if ($zebra) {
        $class .= ' even';
    } else {
        $class .= ' odd';
    }
    return '<li class="' . $class . '">' . $link . $menu . "</li>\n";
}
